<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Point;

class PointController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $param = [];
        $list = Point::orderBy('created_at','desc')->paginate(10);
        return view('admin.point.index',compact('list','param'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.point.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['msg']    = 'Success';
        $rs['status'] = 'success';
        $data = $request->except(['_token']);
        $flag = Point::create($data);
        if($flag) {
            return back()->with('rs',$rs);
        }

        $rs['msg']    = 'Failed';
        $rs['status'] = 'danger';
        return back()->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Point::find($id);
        return view('admin.point.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data   = $request->except(['_token','_method']);
        $flag   = Point::where(['id'=>$id])->update($data);
       
        return redirect('zadmin/points');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $flag   = Point::destroy($id);
       
        return back();
    }

    public function copy(Request $request)
    {
        $id = (int)$request->id;
        $point = Point::find($id);
        $newPoint['lnglat'] = $point->lnglat;
        $data = Point::create($newPoint);
        return view('admin.point.edit',compact('data'));
    }
}
